package com.itheima.controller;

import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author zhangbaiyan
 * @date 2024-10-01 14:57
 * @description:
 * @version:
 */
@Slf4j
@RestController
public class SessionController {

    //设置Cookie
//    @GetMapping("c1")
//    public Result cookie1(HttpServletResponse response){
//        response.addCookie(new Cookie("login_username","zhangsan"));
//        log.info("cookie1");
//        return Result.success();
//    }
//
//    @GetMapping("c2")
//    public Result cookie2(HttpServletRequest request){
//        Cookie[] cookies = request.getCookies();
//        for (Cookie cookie : cookies) {
//            if ("login_username".equals(cookie.getName()))
//
//                System.out.println(cookie.getValue());
//
//        }
//
//        return Result.success();
//    }


    @GetMapping("c1")
    public Result Session1(HttpSession session) {
        log.info("HttpSession-s1:{}", session.hashCode());
        session.setAttribute("loginusername", "zhangsan");

        return Result.success();
    }

    @GetMapping("c2")
    public Result Session2(HttpServletRequest  request) {
        HttpSession session = request.getSession();
        log.info("HttpSession-s2:{}", session.hashCode());
        Object loginusername = session.getAttribute("loginusername");
        log.info("loginusername:{}", loginusername);
        return Result.success(loginusername);

    }
}
